<?php

class Page_EditController extends Crb_Controller
{
    public function preDispatch()
   {
        $this->view->layout()->setLayout('admin');
        $this->view->headScript()->appendFile($this->view->baseUrl('stuff/jquery/jquery.timers.js'));
		parent::preDispatch();
		
        //TODO:  Fancy box install
   }
	
    public function indexAction()
    {
        $pageDAO = new Page();
        $pages = $pageDAO->getAll();
        $this->view->Pages = $pages;
        $this->view->Title = "Список страниц";
    } 
    
    
    public function previewAction()
    {
        $ID = $this->_request->getParam('id',0);
    	$pageDAO = new Page();
        $page = $pageDAO->getOne($ID);
        $this->view->Row = $page; 
    }


    
    public function editAction()
    {
        $this->view->Title = "Редактирование страницы";
        
        $ID = $this->_request->getParam('id',0);
        $Preview = $this->_request->getParam('preview',0);
        $pageDAO = new Page();
        $Row = $pageDAO->getOne($ID);
        
        
        $fileDAO = new File('ostiva');
        $files = $fileDAO->get($ID);
        
        $this->view->files = $files;
        

        if($this->_request->isPost())
        {
            $Title = $this->_request->getParam('title','');
            $Text = $this->_request->getParam('text','');
            $CID = $this->_request->getParam('menuselect',0);
            
            $pageDAO->Edit($ID, $Title, $Text);
            
            $pageDAO->setCategory($ID, $CID);
            
            
/*            if (!empty($_FILES['file']['name']))
            {
                $fileDAO->removeByCategory($ID);
                $fileDAO->uploadFile('file', $ID);
            }*/
            
            
            
            if ($this->_request->getParam('afterSaveAction','list') == "preview")
            {
                $this->_redirect('page/index/view/id/' . $ID);
            } 
            else
            {  
                $this->_redirect('page/edit');
            }
        }
        $this->view->Row = $Row;
    }
    
    
    
    public function addAction()
    {
        $this->view->Title = "Добавление страницы";
        if($this->_request->isPost())
        {
            $Title = $this->_request->getParam('title','');
            $Text  = $this->_request->getParam('text','');
            $CID = $this->_request->getParam('menuselect',0);
            
            
            $pageDAO = new Page();
            $ID = $pageDAO->Add($Title,$Text);
            
//            $News->setCategory($ID, $CID);
            
            
//            $fileDAO = new File('ostiva');
//            $fileDAO->uploadFile('file', $ID);
            
            if ($this->_request->getParam('save_view',''))
            {
                $this->_redirect('page/index/view/id/' . $ID);
            } 
            else
            {  
                $this->_redirect('page/edit');
            }
        }
        
    }
    
    public function removeAction()
    {
        $ID  = $this->_request->getParam('id',0);
        $IDS = $this->_request->getParam('ids',array());
        
        $News = new Page();
        
        foreach ($IDS as $currentID)
        {
            $News->Remove($currentID);
        }
        
        $News->Remove($ID);
        
        $this->_redirect('page/edit');
    }

    
    public function getFilteredPagesAction()
    {
        $filter  = $this->_request->getParam('filter','');
        
        $pageDAO = new Page();
        $pages = $pageDAO->getAllByName($filter);
        $this->view->Pages = $pages;
    } 
    
}

